package com.example.webview;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private WebView web_view;
    private List<Integer> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        list.add(850);
        list.add(500);
        list.add(999);
        list.add(1290);
        list.add(850);
        list.add(1330);
        list.add(1320);
    }

    private void initView() {
        web_view = (WebView) findViewById(R.id.web_view);
        //设置webview支持js
        web_view.getSettings().setJavaScriptEnabled(true);
        //给前端添加js接口
        web_view.addJavascriptInterface(this,"myWebView");
        //设置webview可以打开新窗口
        web_view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        web_view.loadUrl("file:///android_asset/index.html");
        web_view.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress==100)
                {
                    for (Integer integer : list) {

                        web_view.loadUrl("javascript:t_text('"+integer+"')");
                    }
                }
            }
        });
    }
    // 定义JS需要调用的方法
    // 被JS调用的方法必须加入@JavascriptInterface注解
    @JavascriptInterface
    public void getData(String title)
    {
        Log.e("setWeb_view", "setWeb_view: ");
        new AlertDialog.Builder(this)
                .setTitle("测试")
                .setMessage("hhhhhh"+title)
                .setPositiveButton("OK",null)
                .create()
                .show();

    }
    @JavascriptInterface
    public void jump()
    {
        Intent intent=new Intent(MainActivity.this,main2.class);
        startActivity(intent);
    }


}